package com.wd.live.platform;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.annotation.ThreadSafe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;
import us.codecraft.webmagic.utils.FilePersistentBase;

import com.wd.jdbc.LiveStarService;
import com.wd.core.spring.support.SpringUtils;

@ThreadSafe
public class MyJdbcPipeline extends FilePersistentBase implements Pipeline {

	private Logger logger = LoggerFactory.getLogger(getClass());

	/**
	 * create a FilePipeline with default path"/data/webmagic/"
	 */
	public MyJdbcPipeline() {

		setPath("/data/webmagic/");
	}

	public MyJdbcPipeline(String path) {

		setPath(path);
	}

	@SuppressWarnings({ "rawtypes" })
	public void process(ResultItems resultItems, Task task) {

		List<String[]> dataList = new ArrayList<String[]>();
		//解析数据
		try {
			for (Map.Entry<String, Object> entry : resultItems.getAll().entrySet()) {
				if (entry.getValue() instanceof Iterable) {
					Iterable value = (Iterable) entry.getValue();
					for (Object o : value) {
						String[] array = (String[]) o;
						dataList.add(array);
					}
				}
			}
		} catch (Exception e) {
			logger.warn("MyJdbcPipeline error", e);
		}
		//插入数据库
		LiveStarService service = (LiveStarService) SpringUtils.getBean("liveStarService");
		service.insertData(dataList);
	}

	}
